/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */

package cn.knet.sp.ncrm.modules.sys.utils;

import java.util.List;

import cn.knet.sp.ncrm.common.utils.CacheUtils;
import cn.knet.sp.ncrm.common.utils.SpringContextHolder;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.modules.sys.region.dao.RegionDao;
import cn.knet.sp.ncrm.modules.sys.region.entity.Region;
import cn.knet.sp.ncrm.modules.sys.user.entity.User;

/**
 * 区域查询对象
 * @author zhangbin
 * @version 2015 /08/26 15:36:38
 */
public class RegionUtils {
	private static RegionDao regionDao = SpringContextHolder.getBean(RegionDao.class);
	public static final String REGION_CACHE = "regionCache";
	public static final String PREGION_CACHE_NAME = "region_name_";
	public static final String REGION_CACHE_ID = "region_id_";

	public static String getRegionId(String name){
		Region region = getRegion(name);
		if (region != null) {
			return region.getId();
		}
		return null;
	}

	private static Region getRegion(String name) {
		if (StringUtils.isBlank(name)) {
			return null;
		}
		name = StringUtils.trim(name);
		if(name.endsWith("市") || name.endsWith("省")){ //特殊处理以免重复
			name = name.substring(0, name.length() - 1);
		}

		int nameCode = name.hashCode();
		Region entity = (Region) CacheUtils.get(REGION_CACHE, PREGION_CACHE_NAME + nameCode);
		if (entity == null) {
			entity = new Region();
			entity.setRegionName(name);
			entity = regionDao.getByName(entity);
			if (entity == null) {
				entity = new Region();
				entity.setRegionName(name);
				entity.preInsert();//初始化部分参数
				entity.setDateCreated(new java.util.Date());
				entity.setLastUpdated(new java.util.Date());
				entity.setParentId("0");
				entity.setRegionLevel("1");
				entity.setSortOrder("999"); //新增的拍在最前头
				if (entity.getCreateBy() == null || entity.getUpdateBy() == null) {
					entity.setCreateBy(new User("1")); //默认为系统用户
					entity.setUpdateBy(new User("1"));
				}
				regionDao.insert(entity);
			}
			CacheUtils.put(REGION_CACHE, PREGION_CACHE_NAME + nameCode, entity);
			CacheUtils.put(REGION_CACHE, REGION_CACHE_ID + entity.getId(), entity);
			//写入缓存
		}
		return entity;
	}

	public static Region getRegionById(String id) {
		if (StringUtils.isBlank(id)) {
			return null;
		}
		id = StringUtils.trim(id);
		Region enitity = (Region) CacheUtils.get(REGION_CACHE, REGION_CACHE_ID + id);
		if (enitity == null) {
			enitity = regionDao.get(id);
			if (enitity != null) {
				CacheUtils.put(REGION_CACHE, PREGION_CACHE_NAME + enitity.getRegionName().hashCode(), enitity);
				CacheUtils.put(REGION_CACHE, REGION_CACHE_ID + enitity.getId(), enitity);
			}
		}
		return enitity;
	}

	/**
	 * 清除
	 * @author zhangbin
	 * @date 2015 /08/27 11:26:42
	 * @param region the region
	 */
	public static void clearRegion(Region region) {
		CacheUtils.remove(REGION_CACHE, PREGION_CACHE_NAME + region.getRegionName().hashCode());
		CacheUtils.put(REGION_CACHE, PREGION_CACHE_NAME + region.getId());
	}

	public static List listRegion() {
		return regionDao.selectCombinList();
	}


}
